package com.xja.service.impl;

import com.xja.connutil.ConnUtil;
import com.xja.dao.LwlDao;
import com.xja.dao.impl.LwlDaoImpl;
import com.xja.entity.Comment;
import com.xja.entity.Register;
import com.xja.entity.Room;
import com.xja.entity.pojo.RoomAndBills;
import com.xja.entity.pojo.RoomAndIndent;
import com.xja.service.LwlService;

import java.sql.Connection;
import java.sql.SQLException;
import java.util.List;

/**
 * @projectName: HotelManager
 * @package: com.xja.service.impl
 * @className: LwlServiceImpl
 * @author: WenLiang0214
 * @description:
 * @date: 2022/5/5 19:58
 */
public class LwlServiceImpl implements LwlService {
    LwlDao lwlDao =new LwlDaoImpl();
    //根据价格查询房间
    @Override
    public List<Room> findRoomByPrice(int price) {
        List<Room> list =null;
        try {
            list = lwlDao.findRoomByPrice(price);
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }finally {
            ConnUtil.closeConn();
        }
        return list;
    }

    @Override
    public List<Room> findRoom() {
        List<Room> list =null;
        try {
            list = lwlDao.findRoom();
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }finally {
            ConnUtil.closeConn();
        }
        return list;
    }

    @Override
    public List<Room> findRoomByName(String userName)  {
        List<Room> list =null;
        try {
            list = lwlDao.findRoomByName(userName);
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }finally{
            ConnUtil.closeConn();
        }
        return list;
    }

    @Override
    public Integer checkOutById(int roomId) {
        Integer integer=null;
        Connection conn=null;
        try {
            integer = lwlDao.checkOutById(roomId);
        } catch (SQLException throwables) {
            throwables.printStackTrace();
            try {
                conn.rollback();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }finally {
            ConnUtil.closeConn();
        }
        return integer;
    }

    @Override
    public Integer findUserIdByName(String userName) {
        Integer userId=null;
        try {
          userId = lwlDao.findUserIdByName(userName);
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }finally{
            ConnUtil.closeConn();
        }
        return userId;
    }

    @Override
    public List<RoomAndBills> findBillsById(int userId) {
        List<RoomAndBills> list = null;
        try {
            list = lwlDao.findBillsById(userId);
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }finally{
            ConnUtil.closeConn();
        }
        return list;
    }

    @Override
    public Integer estimate(Comment comment) {
        Integer estimate = null;
        try {
            estimate = lwlDao.estimate(comment);
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }finally {
            ConnUtil.closeConn();
        }
        return estimate;
    }

    @Override
    public   List<RoomAndIndent> fidIndent(String findUserName) {
        List<RoomAndIndent> indents = null;
        try {
            indents = lwlDao.fidIndent(findUserName);
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }{
            ConnUtil.closeConn();
        }
        return indents;
    }

    @Override
    public Integer register(Register register) {
        Integer line = null;
        Connection conn=null;
        try {
            line = lwlDao.register(register);
        } catch (SQLException throwables) {
            throwables.printStackTrace();
            try {
                conn.rollback();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }finally {
            ConnUtil.closeConn();
        }
        return line;
    }
    //事务
    /*
    public Integer addStudentAndGrade(Student student,String gradeName){
    Integer i=null;
    Connection conn=null;
    try {
        conn= ConnUtil.getConn();
        conn.setAutoCommit(false);
        Integer gradeId=gd.addGrade(gradeName);
        student.setGradeId(gradeId);
        i=sd.addStudent(student);
        conn.commit();
    } catch (SQLException throwables) {
        throwables.printStackTrace();
        try {
            conn.rollback();
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }finally {
        ConnUtil.closeConn();
    }
    return i;
}
    */
}
